<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="avicit.platform6.commons.utils.ViewUtil"%>
<%@page import="avicit.platform6.core.locale.PlatformLocalesJSTL"%>
<%@page import="java.util.Locale"%>
<%@page import="avicit.platform6.api.session.SessionHelper"%>
<%@page import="avicit.platform6.api.sysshirolog.impl.AfterLoginSessionProcess"%>
<%
    Object lang=session.getAttribute(AfterLoginSessionProcess.SESSION_CURRENT_LANGUAGE_CODE);
    String languageCode="zh_CN";
    if(lang!=null){
        languageCode=lang.toString();
    }
    Locale locale =SessionHelper.getLocaleByUser(request);
%>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>数据库报表配置</title>
    <style type="text/css">
        .required-icon {
            maring: 0px;
            padding: 0px;
            width: 10px;
            height: 23px;
            overflow: hidden;
            display: inline-block;
            vertical-align: top;
            /* opacity: 0.6; */
            /* 	filter: alpha(opacity=60); */
            background:
                    url('static/css/platform/themes/default/public/images/required.gif')
                    no-repeat center center;
        }
    </style>
    <base href="<%=ViewUtil.getRequestPath(request)%>">
    <jsp:include
            page="/avicit/platform6/component/common/EasyUIJsInclude.jsp"></jsp:include>
    <jsp:include page="/avicit/im/common/ImUIJSCSSInclude.jsp"></jsp:include>

    <script type="text/javascript" src="avicit/im/lantop/basic/reportmanager/js/DataBaseSourceConfig.js"></script>
    <script type="text/javascript"
            src="avicit/im/lantop/basic/reportmanager/locale/language_<%=languageCode%>.js"></script>

</head>
<script type="text/javascript">
    var baseurl = "<%=ViewUtil.getRequestPath(request)%>";
    var dialogId = '${dialogId}'; //打开页面传递的模块ID
    var datagridId = "${datagridId}";
    var templateId = "${templateId}";
    var parentId = "${parentId}";
    var sysId = "${sysId}";

    var colNamesDataGrid ;
    var columnsDataGrid ;
    var outputFields = [];
    var fields ;
    var fieldsCount;
    var sql ;

    var nodesFieldIndex = {};

    var tableInfo = {};

    $(function(){
        $('#classTree').tree({
            checkbox : true, /* 是否带复选框 */
            cascadeCheck : false,
            lines : true, /* 线条 */
            method : 'post' /* 请求数据方式 */,
            url : 'platform/LantopArchiveClassController/findChildsByNodeId', /* 请求数据地址 */
            dataType : "json", /* 数据格式 */
            onClick : function(node) {
                // 树节点单击事件
                //onClickRowEvent(node);
            },
            onCheck : function(node, checked){
                var checkNodes = ($('#classTree').tree('getChecked'));
                //console.log(checkNodes);
                var nodes = [];
                $.each(checkNodes, function(index, node){
                    if(node.attributes.ARCHIVE_TYPE == '-1'){
                        nodes.push(node);
                    }
                });
                //console.log(nodes);
                if(nodes.length >= 2){ ;;;
                    var parentNodes = [];
                    $.each(nodes, function(index, node){
                        parentNodes.push($("#classTree").tree('getParent', node.target));
                    });
                    for(var i = 0; i < parentNodes.length - 1 ;i++){
                        if(parentNodes[i] != parentNodes[i + 1]){
                            //'提示', '所选的节点不在同一目录下，不能进行选择！'
                            $.messager.alert("<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>", "<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.nochoose","i18n.lantop",locale)%>", 'warning');
                            $("#classTree").tree('uncheck', node.target);
                            return ;

                        }
                    }
                }

                if(checked){
                    onClickRowEvent(node);
                }else{
                    var nodeId = node.id;
                    var indexScope = nodesFieldIndex[nodeId];
                    if(undefined == indexScope){
                        return ;
                    }else{
                        var index = indexScope.split(',');
                        var start = Number(index[0]);
                        var len = Number(index[1]);
                        if(start + len - 1 > $("#columnsDataGrid").datagrid("getRows").length ){
                            for(var i = 0; i < len ; i ++ ){
                                $("#columnsDataGrid").datagrid("deleteRow",0);
                            }

                        }else{
                            for(var i = start + len - 1; i >= start ; i -- ){
                                $("#columnsDataGrid").datagrid("deleteRow",i);
                            }
                        }
                        $("#columnsDataGrid").datagrid("scrollTo", 0);
                        nodesFieldIndex[nodeId] = undefined;
                    }

                    // 清除关联关系,排序关系的DataGrid
                    $("#realationDatagrid").datagrid('loadData', []);
                    $("#sortDatagrid").datagrid('loadData', []);

                }
            },
            loadFilter : function(data) {
                // 过滤后台返回的数据，处理不同json结构
                if (data.data) {
                    return data.data;
                } else {
                    return data;
                }
            },
            onContextMenu : function(e, node) {
                // 树节点右击菜单
                e.preventDefault();
                $(this).tree('select', node.target);
                showMenu(node, e);
            },
            onLoadSuccess : function() {
                var node = $("#classTree").tree("getRoot");
                if (node != null) {
                    $("#classTree").tree("select", node.target);
                }
            },
            formatter : function(node) {
                return '<span title="' + node.text + '">' + node.text + '</span>';
            }

        });

    });


    // 点击的节点刷新iframe
    function onClickRowEvent(node) {
        if (node.attributes.PARENT_ID == "ROOT") {

            return;
        }
        //	var sysId = node.attributes.SYS_ID;
        //	var nodeName = node.attributes.ARCHIVE_CLASS_NODE_NAME;
        //	var tableName = node.attributes.TABLE_NAME;
        //	var nodeType = "";
        var type = node.attributes.ARCHIVE_TYPE;
        //	var nodeCode = node.attributes.ARCHIVE_CLASS_NODE_CODE;
        //	var nodeId = "";
        // 如果为库表
        if (type == "-1") {
            nodeType = "Y";
            nodeId = node.attributes.PARENT_ID;
            // 显示具体内容
            $("#columnsDataGrid").datagrid("load",{

            });
            $.ajax({
                url : 'platform/ReportFormConfigController/getTableList',
                type : 'post',
                dataType : 'json',
                data : {
                    nodeId: nodeId,
                    templateId : templateId,
                    sysId: sysId
                },
                success : function(result){
                    var len = $('#columnsDataGrid').datagrid('getRows').length;
                    var tt = {};
                    tt[node.id] = (len)  + "," + result.rows.length;
                    $.extend(nodesFieldIndex, tt);

                    // 显示具体内容
                    $.each(result.rows, function(index, row){
                        $("#columnsDataGrid").datagrid("appendRow",row);

                    });

                    $.each(result.rows, function(index, o){

                        var name = o.TABLE_TITLE + "." + o.COL_LABEL;
                        tableInfo[name] =  o.TABLE_NAME + "." + o.COL_NAME;
                    });

                    // 显示已经配置的字段
                    //$.each(result.setFields, function(index, row){
                    //		$("#colNamesDataGrid").datagrid("appendRow",row);
                    //});

                    //$("#colNamesDataGrid").datagrid("loadData", result.setFields);
                    // 填入实际输出字段内容
                    //getCurrColumnForCurrOutput();

                }
            });
        }
    }

    function formatterField(value,row,index){
        var colName = row.COL_NAME;
        if(colName == undefined || colName == null || colName == "" ){
            return value;
        }
        return row.TABLE_TITLE + "." + value;
    }



</script>
<body class="easyui-layout" data-options="fit:true, border:false">

<div data-options="region:'center',border:false">

    <div id="cc" class="easyui-layout" fit='true' style="">
        <%--关联及排序选项 --%>
        <div data-options="region:'south',title:'<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.associationandsortingoptions","i18n.lantop",locale)%>',split:true,border:false" style="height:200px;">
            <div id="tt" class="easyui-tabs" data-options="border:'false', fit:true">

                <!-- datagrid 工具栏 -->
                <div id="tb1" style="overflow: hidden; font-size: 0;">
                    <sec:accesscontrollist hasPermission="3"
                                           domainObject="usercustomdatasource_table_structure_button_addField">
                        <%--添加 --%>
                        <a class="easyui-linkbutton" plain="true" iconCls="icon-im-add"
                           onclick="append();" href="javascript:void(0);" id="addTempField"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.add","i18n.lantop",locale)%></a>
                    </sec:accesscontrollist>
                    <sec:accesscontrollist hasPermission="3"
                                           domainObject="usercustomdatasource_table_structure_button_deleteField">
                        <%--删除 --%>
                        <a class="easyui-linkbutton" plain="true" iconCls="icon-im-remove"
                           onclick="removeit();" href="javascript:void(0);"
                           id="deleteTempField"><%=PlatformLocalesJSTL.getBundleValue("lantop.delete","i18n.lantop",locale)%></a>
                    </sec:accesscontrollist>

                </div>
                <%--关联 --%>
                <div title="<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.association","i18n.lantop",locale)%>" data-options="closable:false,border:false"
                     style="overflow: hidden;" id="tabDataSource">
                    <table id="realationDatagrid" class="easyui-datagrid"
                           data-options="
						collapsible: false,
						fitColumns: true,
						autoRowHeight: false,
						singleSelect: true,
						striped:false,
						pagination : false,
						singleSelect: false,
	     				fit:true,
	     				border:false,
	     				onClickCell : onClickCell,
						method: 'post',
						toolbar : '#tb1',
					">
                        <thead>
                        <!--  formatter:fomaterColLabel -->
                        <tr>
                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_id">
                                <%--主键ID --%>
                                <th data-options="field:'ID', halign:'center',checkbox:true"
                                    title="主键ID" width="20"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.pkid","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>

                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_id">
                                <%--著录项名称 --%>
                                <th data-options="field:'COLNAME', halign:'center'" title=""
                                    width="100"> <span
                                        class="required-icon"></span><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.cataloguingname","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>

                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_colLabel">
                                <%--连接符 --%>
                                <th
                                        data-options="field:'OPRATION',halign:'center', align:'center', editor:{type:'combobox',options:{required:true,valueField:'op',textField:'opName',data:[{op:'==',opName:'='}],editable:false}},"
                                        width="100" title="连接符"> <span
                                        class="required-icon"></span><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.connector","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>
                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_colName">
                                <%--著录项名称 --%>
                                <th
                                        data-options="field:'COLNAME1', halign:'center', align:'center'"
                                        width="100" title="著录项名称"><span
                                        class="required-icon"></span><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.cataloguingname","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>


                        </tr>
                        </thead>
                    </table>

                </div>
                <!--  排序tab -->
                <%--排序 --%>
                <div title="<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.order","i18n.lantop",locale)%>" data-options="closable:false, border:false"
                     style="overflow: hidden; background-color: #E3EFFB; padding: 0px;"
                     id="tabSQL">
                    <!-- datagrid 工具栏 -->
                    <div id="tb2" style="overflow: hidden; font-size: 0;">
                        <sec:accesscontrollist hasPermission="3"
                                               domainObject="usercustomdatasource_table_structure_button_addField">
                            <%--添加 --%>
                            <a class="easyui-linkbutton" plain="true" iconCls="icon-im-add"
                               onclick="appendSort();" href="javascript:void(0);"
                               id="addTempField"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.add","i18n.lantop",locale)%></a>
                        </sec:accesscontrollist>
                        <sec:accesscontrollist hasPermission="3"
                                               domainObject="usercustomdatasource_table_structure_button_deleteField">
                            <%--删除 --%>
                            <a class="easyui-linkbutton" plain="true"
                               iconCls="icon-im-remove" onclick="removeitSort();"
                               href="javascript:void(0);" id="deleteTempField"><%=PlatformLocalesJSTL.getBundleValue("lantop.delete","i18n.lantop",locale)%></a>
                        </sec:accesscontrollist>

                    </div>
                    <table id="sortDatagrid" class="easyui-datagrid"
                           data-options="
						collapsible: false,
						fitColumns: true,
						autoRowHeight: false,
						singleSelect: true,
						striped:false,
						singleSelect: false,
						border:false,
	     				fit:true,
	     				onClickCell : onClickCell,
						method: 'post',
						toolbar : '#tb2',
					">
                        <thead>
                        <!--  formatter:fomaterColLabel -->
                        <tr>
                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_id">
                                <%--主键ID --%>
                                <th data-options="field:'ID', halign:'center',checkbox:true"
                                    title="主键ID" width="20"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.pkid","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>

                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_id">
                                <%--著录项名称 --%>
                                <th data-options="field:'COLNAME3', halign:'center'" title=""
                                    width="100"><span
                                        class="required-icon"></span><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.cataloguingname","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>

                            <sec:accesscontrollist hasPermission="3"
                                                   domainObject="commtablemanager_table_structure_datagrid_colLabel">
                                <%--排序类型 --%>
                                <th data-options="field:'SORT',halign:'center', align:'center', editor:{type:'combobox',options:{onSelect : typeSelectSort, required:true,tipPosition : 'left',valueField:'op',textField:'opName',data:[{op:'升序',opName:'升序'},{op:'降序',opName:'降序'}],editable:false}},"
                                    width="100" title="排序类型"><span
                                        class="required-icon"></span><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.ordertype","i18n.lantop",locale)%></th>
                            </sec:accesscontrollist>

                        </tr>
                        </thead>
                    </table>

                </div>
            </div>
        </div>

        <%--数据源 --%>
        <div data-options="region:'west',title:'<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.datasource","i18n.lantop",locale)%>',split:true,border:false" style="width:325px;">
            <ul id="classTree"></ul>
        </div>
        <%--数据表字段--%>
        <div data-options="region:'center',title:'<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.datatablefield","i18n.lantop",locale)%>',border:true" style="padding:5px;background:#eee;">

            <table id="columnsDataGrid" class="easyui-datagrid"
                   data-options="
								fit:true,
								collapsible: false,
								fitColumns: true,
								autoRowHeight: false,
								singleSelect: false,
								striped:true,
								rownumbers: true,
								method: 'post',
								rownumbers: true,

							">
                <thead>
                <tr>
                    <sec:accesscontrollist hasPermission="3"
                                           domainObject="commtablemanager_table_structure_datagrid_id">
                        <%--主键ID --%>
                        <th data-options="field:'ID', halign:'center',checkbox:true"
                            title="主键ID" width="20"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.pkid","i18n.lantop",locale)%></th>
                    </sec:accesscontrollist>
                    <sec:accesscontrollist hasPermission="3" domainObject="excel_export_datagrid_id">
                        <%--表名 --%>
                        <th data-options="field:'TABLE_NAME',  hidden:true , halign:'center', align:'center', sortable:false" width="200" title="表名"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.tablename","i18n.lantop",locale)%></th>
                    </sec:accesscontrollist>

                    <sec:accesscontrollist hasPermission="3" domainObject="excel_export_datagrid_id">
                        <%--字段名称--%>
                        <th data-options="field:'COL_NAME',  hidden:true , halign:'center', align:'center', sortable:false" width="200" title="字段名称"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.filedname","i18n.lantop",locale)%></th>
                    </sec:accesscontrollist>

                    <sec:accesscontrollist hasPermission="3" domainObject="excel_export_datagrid_id">
                        <%--字段名称--%>
                        <th data-options="field:'TABLE_TITLE', hidden:true , halign:'center', align:'center', sortable:false" width="200" title="字段名称"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.filedname","i18n.lantop",locale)%></th>
                    </sec:accesscontrollist>

                    <sec:accesscontrollist hasPermission="3" domainObject="excel_export_column_datagrid_colName">
                        <%--字段名称--%>
                        <th data-options="title:'', field:'COL_LABEL', halign:'center', align:'center', sortable:false,formatter:formatterField" width="200" title="字段名称"><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesourceconfig.filedname","i18n.lantop",locale)%></th>
                    </sec:accesscontrollist>
                </tr>
                </thead>
            </table>


        </div>
    </div>


</div>



</body>
</html>